import React, { Component, PropTypes } from 'react';
import { Router, Route, IndexRoute, browserHistory, Link,hashHistory } from 'react-router';
import {Select,Tag} from 'antd';
import {Tool} from './Tool';
import * as config from './Config';
import {Api} from "./Api";
const Util = {};
const {target,pathImage} = config;
const Option = Select.Option;

/* 验证是否为空 */
Util.IsNull = function (obj) {
    var flag = false;
    if (obj == null || obj == undefined || typeof (obj) == 'undefined' || obj == '') {
        flag = true;
    } else if (typeof (obj) == 'string') {
        obj = obj.trim();
        if (obj == '') {//为空
            flag = true;
        } else {//不为空
            obj = obj.toUpperCase();
            if (obj == 'NULL' || obj == 'UNDEFINED' || obj == '{}') {
                flag = true;
            }
        }
    }
    else {
        flag = false;
    }
    return flag;
}


Util.Trim = function(str,is_global){
    var result;
    result = str.replace(/(^\s+)|(\s+$)/g,"");
    if(is_global.toLowerCase()=="g"){
        result = result.replace(/\s/g,"");
    }
    return result;
}

Util.loginOut = function(){
    Tool.removeLocalItem('company');
    Tool.removeLocalItem('User');
    Tool.removeLocalItem('fieldType');
    Tool.removeLocalItem('tagList');
    Tool.removeLocalItem("domainArray");
    Tool.removeLocalItem("count");
}

/*
 * 验证是否为手机号
 * */
Util.Phone= function (obj) {
    if(Util.IsNull(obj)){
        return false;
    }
    if(!(/^1(3|4|5|7|8)\d{9}$/.test(obj))){
        return false;
    }
    return true;
}

Util.getApi = (key)=> {
    let obj=null;
    for(let i=0,len=Api.length;i<len;i++){
        if(Api[i].key==key){
            obj=Api[i];
            return obj.url ;
        }
    }
};

Util.getField=()=>{
    return [
        {title:"电子电气",id:7,isActive:false},
        {title:"纺织服装",id:8,isActive:false},
        {title:"化学化工",id:9,isActive:false},
        {title:"计算机",id:10,isActive:false},
        {title:"能源环保",id:11,isActive:false},
        {title:"机械设备",id:12,isActive:false},
        {title:"家用电器",id:13,isActive:false},
        {title:"建筑建材",id:14,isActive:false},
        {title:"农林牧渔",id:15,isActive:false},
        {title:"轻工制造",id:16,isActive:false},
        {title:"食品饮料",id:17,isActive:false},
        {title:"新材料",id:18,isActive:false},
        {title:"生物医药",id:19,isActive:false},
        {title:"其他",id:20,isActive:false}
    ];
}

Util.initChangeSelect=(obj)=>{
    var strArr = [];
    if(obj && obj.length>0){
        obj.map(d=>{
            strArr.push(d.issueId);
        })
    }
    return strArr;
}

Util.getFieldById=(id)=>{
    var title = "",nId = 0;
    Util.getField().map(function(i){
        if(i.id==id){
            title = i.title;
        }
    })
    return title;
}

Util.getFieldByName=(name)=>{
    var id = 0;
    Util.getField().map(function(i){
        if(i.title==name){
            id = i.id;
        }
    })
    return id;
}

Util.getHonors=()=>{
    return [
        {title:"中国科学院院士",id:1,isActive:false},
        {title:"中国工程院院士",id:2,isActive:false},
        {title:"长江学者",id:3,isActive:false},
        {title:"千人计划",id:4,isActive:false},
        {title:"青年千人计划",id:5,isActive:false},
        {title:"青年长江学者",id:6,isActive:false},
        {title:"国家杰出青年科学基金",id:7,isActive:false},
        {title:"优秀青年基金",id:8,isActive:false},
        {title:"百千万人才工程",id:9,isActive:false},
        {title:"新世纪优秀人才	",id:10,isActive:false},
    ];
}

Util.getHonorsById=(id)=>{
    var title = "";
    Util.getHonors().map(function(i){
        if(i.id==id){
            title = i.title;
        }
    })
    return title;
}

Util.getHonorsByName=(name)=>{
    var id = 0;
    Util.getHonors().map(function(i){
        if(i.title==name){
            id = i.id;
        }
    })
    return id;
}

Util.IsNullJson=(rowData)=>{
    if(JSON.stringify(rowData) == "{}"){
        return true;
    }else{
        return false;
    }
}


Util.isLogin=(type,old)=>{
    //实时判断是否登录状态
    var token = JSON.parse(Tool.localItem("User")) ? JSON.parse(Tool.localItem("User")).token : '';
    var flag=true;
    if(!(token && token.length>0)){//为空，去登录
        if(type==1){
            return false;
        }else{
            hashHistory.push("/login");
            return false;
        }
    }
    return flag;
}

Util.ComUserLogin=()=>{
    var nowUrl = window.location.href;
    console.log("链接"+nowUrl)
    if(!Util.isLogin(1,0)){
        if(!(nowUrl.indexOf("/login")>-1)){
            hashHistory.push("/login");
        }
    }
}

Util.paginationInit=(list)=>{
    var pagination = {
        defaultCurrent:1,current:1,
        total:0,pageSize:10,
        showSizeChanger:true
    }
    if(list && list.content && list.content.length>0){
        pagination.total = list.totalElements;
        pagination.pageSize = list.size;
        pagination.current = list.number+1;
    }
    return pagination;
}

Util.getSpeed=(status)=>{
    var str="未知";
    if(status==0){
        str="待核实";
    }else if(status==1){
        str="已核实";
    }else if(status==2){
        str="已匹配，待推送";
    }
    else if(status==3){
        str="已推送";
    }
    else if(status==4){
        str="拟定合同";
    }
    else if(status==5){
        str="签订合同";
    }
    else if(status==6){
        str="信息入库";
    }
    else if(status==7){
        str="需求响应";
    }
    return str;
}
Util.getSpeedInfo=(status)=>{
    var str="未知";
    if(status==0){
        str="创建成功";
    }else if(status==1){
        str="核实完毕：";
    }else if(status==2){
        str="已匹配，待推送";
    }
    else if(status==3){
        str="已推送";
    }
    else if(status==4){
        str="拟定合同";
    }
    else if(status==5){
        str="签订合同";
    }
    else if(status==6){
        str="信息入库";
    }
    else if(status==7){
        str="需求响应";
    }
    return str;
}

Util.getSpeedTag=(status)=>{
    var str="未知";
    if(status==0){
        return (
            <Tag color="purple-inverse">待核实</Tag >
        )
    }else if(status==1){
        return (
            <Tag color="blue">已核实</Tag >
        )
    }else if(status==2){
        return (
            <Tag color="cyan-inverse">已匹配，待推送</Tag >
        )
    }
    else if(status==3){
        return (
            <Tag color="green-inverse">已推送</Tag >
        )
    }
    else if(status==4){
        return (
            <Tag color="orange-inverse">拟定合同</Tag >
        )
    }
    else if(status==5){
        return (
            <Tag color="red-inverse">签订合同</Tag >
        )
    }
    else if(status==6){
        return (
            <Tag color="pink-inverse">信息入库</Tag >
        )
    }
    else if(status==7){
        return (
            <Tag color="blue-inverse">需求响应</Tag >
        )
    }
    return (
        <Tag color="#999">{str}</Tag >
    )
}

/* saas */
Util.getSaasSpeed=(record)=>{
    var str="未知";
    var status = record.project_status;
    if(status==0){
        str="创建成功";
    }else if(status==1){
        str="需求导入成功";
    }else if(status==2){
        str="核实情况("+record.realIssueCount+"/"+record.issueCount+")";
    }else if(status==3){
        str="推送情况("+record.pushIssueCount+"/"+record.issueCount+")";
    }
    return str;
}

/* admin */
Util.getAdminSpeed=(record)=>{
    var str="未知";
    var status = record.project_status;
    if(status==0){
        str="待录入";
    }else if(status==1){
        str="录入完毕";
    }else if(status==2){
        str="审核完毕";
    }else if(status==3){
        str="已推送";
    }
    return str;
}

/* 需求核实进度 */
Util.getConfirm=(status)=>{
    var str="未知";
    if(status==2){
        str="未核实";
    }else if(status==1){
        str="需求真实";
    }else if(status==0){
        str="无需对接";
    }else if(status==3){
        str="待确认";
    }
    return str;
}

Util.getCompanyType=()=>{
    return [
        {title:"国有企业",id:1,isActive:false},
        {title:"国有控股企业",id:2,isActive:false},
        {title:"外资企业",id:3,isActive:false},
        {title:"合资企业",id:4,isActive:false},
        {title:"私营企业",id:5,isActive:false},
        {title:"事业单位",id:6,isActive:false},
        {title:"国家行政机关",id:7,isActive:false},
        {title:"政府",id:8,isActive:false},
    ];
}

Util.toString=(val,def)=>{
    if(Util.IsNull(val)){
       return def?def:"——";
    }
    return val;
}

Util.getYear=(startYear,type)=>{
    var now = 2017;var yearOption = [];
    if(type==0){
        yearOption.push(<Option value={""} key="0">开始时间</Option>);
    }else{
        yearOption.push(<Option value={""} key="0">结束时间</Option>);
    }
    if(startYear<1){
        return yearOption;
    }
    for(var i=now;i>startYear-1;i--){
        yearOption.push(<Option value={i+""} key={i}>{i}</Option>);
    }
    return yearOption;
}

Util.initPrivate=(obj)=>{
    var arr = [];
    if(obj && obj.length > 0){
        obj.map(function (item) {
            arr.push(item.id)
        })
    }
    return arr;
}
/* 判断是否有权限操作 */
Util.hasPermission=(id)=>{
    var arr = Tool.getSession("userPrivate") || [];
    var flag = false;
    if(arr && arr.length > 0){
        arr.map(function (it) {
            if(it == id){
                flag = true;
            }
        })
    }
    return flag;
}

export default Util;
